package com.gxa.service.Impl;

import com.gxa.common.JsonResult;
import com.gxa.common.R;
import com.gxa.mapper.MonthGoalMapper;
import com.gxa.pojo.Monthly_Goals;
import com.gxa.pojo.Monthly_Goals_Son;
import com.gxa.pojo.Monthly_Goals_Template;
import com.gxa.pojo.goalPojo.GoalPojo;
import com.gxa.pojo.goalPojo.GoalsPojo;
import com.gxa.pojo.goalPojo.Monthly_goals;
import com.gxa.service.MonthGoalService;
import com.gxa.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class MonthGoalServiceImpl implements MonthGoalService {

    @Autowired(required = false)
    private MonthGoalMapper monthGoalMapper;

    private String Title;
    @Transactional
    @Override
    public R EditGoals(Monthly_Goals_Template mgt) {
        R r= new R();
        if(mgt!=null){
            List<Monthly_Goals_Template> monthly_goals_templateList=monthGoalMapper.queryGoal(mgt.getGoal_thead1(),mgt.getGoal_thead2(),mgt.getGoal_thead3(),mgt.getGoal_thead4(),mgt.getGoal_thead5(),mgt.getGoal_thead6());
            Iterator<Monthly_Goals_Template> it=monthly_goals_templateList.iterator();
            if(!it.hasNext()){
                int result = monthGoalMapper.editGoal(mgt.getGoal_thead1(),mgt.getGoal_thead2(),mgt.getGoal_thead3(),mgt.getGoal_thead4(),mgt.getGoal_thead5(),mgt.getGoal_thead6());
                System.out.println(result);
                if(result > 0){
                    r.setCode(200);
                    r.setMsg("操作成功");
                }
            }else {
                int resulr01= monthGoalMapper.deleteGoal(mgt.getGoal_thead1(),mgt.getGoal_thead2(),mgt.getGoal_thead3(),mgt.getGoal_thead4(),mgt.getGoal_thead5(),mgt.getGoal_thead6());
                int result02 = monthGoalMapper.editGoal(mgt.getGoal_thead1(),mgt.getGoal_thead2(),mgt.getGoal_thead3(),mgt.getGoal_thead4(),mgt.getGoal_thead5(),mgt.getGoal_thead6());
                r.setCode(200);
                r.setMsg("该表头以存在，请直接使用");
            }
            return r;
        }
        r.setCode(200);
        r.setMsg("传值失败");

        return r;
    }

    @Override
    public R FindGoals(){
        R r=new R();
        List<Monthly_Goals_Template> monthly_goals_templateList= monthGoalMapper.findGoal();
        System.out.println(monthly_goals_templateList.toArray().toString());
        Iterator<Monthly_Goals_Template> it=monthly_goals_templateList.iterator();
        if (it.hasNext()){
            r.setCode(200);
            r.setMsg("success");
            r.setData(monthly_goals_templateList);
          }
        return r;
    }

    @Override
    public R FindTitles(String emp_name) {
        R r=new R();
            List<GoalPojo> monthly_goals_templateList= monthGoalMapper.findTitles();
            Iterator<GoalPojo> it=monthly_goals_templateList.iterator();
            List<GoalsPojo> goalsPojos=new ArrayList<>();
            if (it.hasNext()){
                GoalsPojo goalsPojo=new GoalsPojo();
                goalsPojo.setEmp_name(emp_name);
                goalsPojo.setEntry_Date(DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss"));
                goalsPojos.add(goalsPojo);
            }
            r.setCode(200);
            r.setMsg("success");
            r.setData(goalsPojos);

        return r;
    }

    @Transactional
    @Override
    public R EidtTitles(GoalPojo goalPojo,Date time) {
        R r=new R();
        if(goalPojo!=null){
            if(goalPojo.getGoal_Thead1()!=null&&!"".equals(goalPojo.getGoal_Thead1())) {
                List<Monthly_Goals_Template> monthly_goals_templateList = monthGoalMapper.queryGoal(goalPojo.getGoal_Thead1(), goalPojo.getGoal_Thead2(), goalPojo.getGoal_Thead3(), goalPojo.getGoal_Thead4(), goalPojo.getGoal_Thead5(), goalPojo.getGoal_Thead6());
                Iterator<Monthly_Goals_Template> it = monthly_goals_templateList.iterator();
                if (!it.hasNext()) {
                    int result = monthGoalMapper.editGoal(goalPojo.getGoal_Thead1(), goalPojo.getGoal_Thead2(), goalPojo.getGoal_Thead3(), goalPojo.getGoal_Thead4(), goalPojo.getGoal_Thead5(), goalPojo.getGoal_Thead6());
                    System.out.println(result);
                    if (result > 0) {
                        r.setCode(200);
                        r.setMsg("操作成功");
                    }
                } else {
                    int resulr01 = monthGoalMapper.deleteGoal(goalPojo.getGoal_Thead1(), goalPojo.getGoal_Thead2(), goalPojo.getGoal_Thead3(), goalPojo.getGoal_Thead4(), goalPojo.getGoal_Thead5(), goalPojo.getGoal_Thead6());
                    int result02 = monthGoalMapper.editGoal(goalPojo.getGoal_Thead1(), goalPojo.getGoal_Thead2(), goalPojo.getGoal_Thead3(), goalPojo.getGoal_Thead4(), goalPojo.getGoal_Thead5(), goalPojo.getGoal_Thead6());
                    r.setCode(200);
                    r.setMsg("该表头以存在，请直接使用");
                }
            }
            List<Monthly_Goals_Son> list=monthGoalMapper.queryTitles(goalPojo.getTitle());
            Iterator<Monthly_Goals_Son> it=list.iterator();
            if(!it.hasNext()){
                System.out.println("no");
                int result=monthGoalMapper.addMonthTitle(goalPojo.getEmp_name(),goalPojo.getTitle(),time);
                if (result>0){
                    int result02=monthGoalMapper.addMonthTitleContent(goalPojo.getGoal_Content1(),goalPojo.getGoal_Content2(),goalPojo.getGoal_Content3(),goalPojo.getGoal_Content4(),goalPojo.getGoal_Content5(),goalPojo.getGoal_Content6());
                    if (result02>0){
                        r.setCode(200);
                        r.setMsg("保存成功");
                    }
                }
            }else {
                r.setCode(200);
                r.setMsg("改标题已存在");
            }
        }
        r.setCode(200);
        r.setMsg("内容为空");
        return r;
    }

    @Override
    public JsonResult queryActivitys(Integer currentPage, Integer pageSize) {
        JsonResult r=new JsonResult();
        if(currentPage>-1&&pageSize>-1){
            int param1=(currentPage-1)*pageSize;
            int param2=pageSize;
            List<Monthly_Goals> monthly_goalsList=monthGoalMapper.queryActivity(param1,param2);
            List<Monthly_goals> monthly_goals=new ArrayList<>();
            for (Monthly_Goals monthly_goals1:monthly_goalsList){
                String s= DateUtils.date2String(monthly_goals1.getEntry_date(),"yyyy-MM-dd");
                Monthly_goals mgs=new Monthly_goals();
                mgs.setMonthly_goals_id(monthly_goals1.getMonthly_goals_id());
                mgs.setCounts(monthly_goals1.getCounts());
                mgs.setEmp_name(monthly_goals1.getEmp_name());
                mgs.setTitle(monthly_goals1.getTitle());
                mgs.setEntry_date(s);
                monthly_goals.add(mgs);
            }
            int resullt =monthGoalMapper.countQuery();
            r.setCode(200);
            r.setMsg("success");
            r.setCount(resullt);
            r.setData(monthly_goals);
        }else{
            r.setCode(500);
            r.setMsg("页码数不对");
        }

        return r;
    }

    @Override
    public R editActivityQuery(String title) {
        Title=title;
       R r=new R();
      if(title!=null&&!"".equals(title)){
          List<GoalPojo> monthly_goalsList=monthGoalMapper.queryEditActivity(title);
          Iterator<GoalPojo> it=monthly_goalsList.iterator();
          List<GoalsPojo> goalsPojos=new ArrayList<>();
          if (it.hasNext()){
              for (GoalPojo goalPojo:monthly_goalsList){
                  String s= DateUtils.date2String(goalPojo.getEntry_Date(),"yyyy-MM-dd");
                  GoalsPojo goalsPojo=new GoalsPojo();
                  goalsPojo.setTitle(goalPojo.getTitle());
                  goalsPojo.setEmp_name(goalPojo.getEmp_name());
                  goalsPojo.setEntry_Date(s);
                  goalsPojo.setGoal_Content1(goalPojo.getGoal_Content1());
                  goalsPojo.setGoal_Content2(goalPojo.getGoal_Content2());
                  goalsPojo.setGoal_Content3(goalPojo.getGoal_Content3());
                  goalsPojo.setGoal_Content4(goalPojo.getGoal_Content4());
                  goalsPojo.setGoal_Content5(goalPojo.getGoal_Content5());
                  goalsPojo.setGoal_Content6(goalPojo.getGoal_Content6());
                  goalsPojo.setGoal_Thead1(goalPojo.getGoal_Thead1());
                  goalsPojo.setGoal_Thead2(goalPojo.getGoal_Thead2());
                  goalsPojo.setGoal_Thead3(goalPojo.getGoal_Thead3());
                  goalsPojo.setGoal_Thead4(goalPojo.getGoal_Thead4());
                  goalsPojo.setGoal_Thead5(goalPojo.getGoal_Thead5());
                  goalsPojo.setGoal_Thead6(goalPojo.getGoal_Thead6());
                  goalsPojos.add(goalsPojo);
              }
          }
          r.setCode(200);
          r.setMsg("success");
          r.setData(goalsPojos);
      }
      r.setCode(200);
      r.setMsg("标题为空");
       return r;
    }

    @Transactional
    @Override
    public R editActivity(GoalPojo goalPojo) {
        System.out.println(goalPojo.toString());
        R r = new R();
        if(goalPojo!=null){
            List<Monthly_Goals> monthly_goalsList =monthGoalMapper.editTitleQuery(goalPojo.getTitle());
            if (!monthly_goalsList.isEmpty()){
                int result=monthGoalMapper.updateContent(goalPojo.getGoal_Content1(), goalPojo.getGoal_Content2(), goalPojo.getGoal_Content3(), goalPojo.getGoal_Content4(), goalPojo.getGoal_Content5(), goalPojo.getGoal_Content6(),goalPojo.getTitle());
                if (result>0) {
                    r.setCode(200);
                    r.setMsg("success");
                }
            }
            else{
                System.out.println(Title);
                monthGoalMapper.updateActivityGoal(goalPojo.getTitle(),Title);
                int result= monthGoalMapper.updateContent(goalPojo.getGoal_Content1(), goalPojo.getGoal_Content2(), goalPojo.getGoal_Content3(), goalPojo.getGoal_Content4(), goalPojo.getGoal_Content5(), goalPojo.getGoal_Content6(),goalPojo.getTitle());
                r.setCode(200);
                r.setMsg("success");
            }
        }
        else{
            r.setCode(500);
            r.setMsg("插入失败");
        }
        return r;
    }

    @Override
    public JsonResult goalWordSelect(String keyword, Integer currentPage, Integer pageSize) {
        JsonResult r=new JsonResult();
        System.out.println(keyword);
        if(currentPage!=null){
            currentPage=(currentPage-1)*pageSize;
            if(keyword!=null){
                List<Monthly_Goals> monthly_goalsList=monthGoalMapper.wordSelect(keyword,currentPage,pageSize);
                List<Monthly_goals> monthly_goals=new ArrayList<>();
                for (Monthly_Goals monthly_goals1:monthly_goalsList){
                    String s= DateUtils.date2String(monthly_goals1.getEntry_date(),"yyyy-MM-dd");
                    Monthly_goals mgs=new Monthly_goals();
                    mgs.setMonthly_goals_id(monthly_goals1.getMonthly_goals_id());
                    mgs.setCounts(monthly_goals1.getCounts());
                    mgs.setEmp_name(monthly_goals1.getEmp_name());
                    mgs.setTitle(monthly_goals1.getTitle());
                    mgs.setEntry_date(s);
                    monthly_goals.add(mgs);
                    int result =monthGoalMapper.countQuery();
                    r.setCode(200);
                    r.setMsg("success");
                    r.setCount(result);
                    r.setData(monthly_goals);
                }
            } else{
                List<Monthly_Goals> monthly_goals=monthGoalMapper.wordQueryActivity(currentPage,pageSize);
                int result=monthGoalMapper.countQuery();
                r.setCode(200);
                r.setMsg("success");
                r.setCount(result);
                r.setData(monthly_goals);
            }
        }
        return r;
    }

    @Transactional
    @Override
    public JsonResult deleteActivity(String title) {
        JsonResult r=new JsonResult();
        if(title!=null&&!"".equals(title)){
            List<Monthly_Goals_Son> list=monthGoalMapper.queryTitles(title);
            Iterator<Monthly_Goals_Son> it=list.iterator();
            if(it.hasNext()){
                int result02=monthGoalMapper.deleteActivityContent(title);
                int result01= monthGoalMapper.deleteActivityGoal(title);
            }
            int result =monthGoalMapper.countQuery();
            r.setCode(200);
            r.setMsg("删除成功");
            r.setCount(result);
            r.setData(null);
        }
        return r;
    }
}
